package com.baihan.angus.app.game.basic.views;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.baihan.angus.app.game.basic.R;
import com.baihan.angus.app.game.basic.utils.AppUtil;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

public class LoadingDialog extends Dialog {

    private Map<Long, String> mTags = new ConcurrentHashMap<>();
    private WindowManager.LayoutParams mLayoutParams;
    private Context mContext;
    private GifImageView gifImageView;
//    private ImageView mImageView;
//    private AnimationDrawable mAnimationDrawable;

    public LoadingDialog(@NonNull Context context) {
        super(context, R.style.TransparentBackgroundDialog);
        mContext = context;
        initContentView();
        init();
    }

    protected void init() {
        Window window = this.getWindow();
        this.mLayoutParams = window.getAttributes();
        if (this.mLayoutParams != null) {
            this.mLayoutParams.height = AppUtil.getScreenHeight(mContext);
            this.mLayoutParams.width = -1;
            this.mLayoutParams.gravity = 17;
        }
        window.setAttributes(this.mLayoutParams);
    }

    protected void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_gif_loading, (ViewGroup) null);
        this.setContentView(dialogView);
        this.setCanceledOnTouchOutside(false);

//        mImageView = dialogView.findViewById(R.id.imageView);
//        mImageView.setImageResource(R.drawable.anim_page_loading);
//        mAnimationDrawable = (AnimationDrawable) mImageView.getDrawable();

        gifImageView = dialogView.findViewById(R.id.gifImageView);
        try {
            final GifDrawable gifDrawable = new GifDrawable(mContext.getResources(), R.drawable.content_loading1);
//            gifDrawable.addAnimationListener(new AnimationListener() {
//                @Override
//                public void onAnimationCompleted(int loopNumber) {
//                    gifDrawable.stop();
//                }
//            });
            gifImageView.setImageDrawable(gifDrawable);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void showDialog(String tag) {
        mTags.put(System.currentTimeMillis(), tag);
        if (!this.isShowing()) {
//            if (mAnimationDrawable != null && !mAnimationDrawable.isRunning()) {
//                mAnimationDrawable.start();
//            }
            show();
        }
    }

    public void hideDialog(String tag) {
        Iterator<Map.Entry<Long, String>> it = mTags.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Long, String> entry = it.next();
            if (entry.getValue().equals(tag)) {
                it.remove();//使用迭代器的remove()方法删除元素
            }
        }
        if (mTags.size() == 0 && this.isShowing()) {
            this.cancel();
        }
//        if (mAnimationDrawable.isRunning()) {
//            mAnimationDrawable.stop();
//        }
    }

}
